package com.chem.lems.serves.impl;

import com.chem.lems.constans.ResultCodeConstant;
import com.chem.lems.entity.Menus;
import com.chem.lems.entity.Role;
import com.chem.lems.repository.MenusRepository;
import com.chem.lems.repository.RoleRepository;
import com.chem.lems.serves.RoleServies;
import com.chem.lems.utils.ResultUtil;
import com.chem.lems.vo.RoleVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class RoleServiesImpl implements RoleServies {
    @Autowired
    private MenusRepository menusRepository;
    @Autowired
    private RoleRepository roleRepository;
    @Autowired
    private MenusServiesImpl menusServies;

    /**
     * 获取当前角色的菜单权限列表
     *
     * @param roleId
     * @return
     */
    @Override
    public ResultUtil tree(String roleId) {
        List<Menus> list = menusRepository.findAllById(menusRepository.findMenusByRole(roleId));
        List tree = new ArrayList<>(menusServies.formatMenusTree(list).values());
        Collections.sort(tree);
        return new ResultUtil(ResultCodeConstant.SUCCESS, "查询成功", tree);
    }


    /**
     * 获取全部角色信息以及对应的权限
     *
     * @return
     */
    public ResultUtil gitRoles() {
        List<Role> list = roleRepository.findAll();
        List<RoleVO> roleVOList = new ArrayList<>();
        for (Role role : list) {
            List<Menus> menusList = role.getMenus();
            List children = new ArrayList();
            if (!menusList.isEmpty() && menusList.size() > 0) {
                children = new ArrayList<>(menusServies.formatMenusTree(menusList).values());
            }
            RoleVO roleVO = new RoleVO(role.getRoleId(), role.getRoleName(), role.getRoleDesc(), children);
            roleVOList.add(roleVO);
        }
        return new ResultUtil(ResultCodeConstant.SUCCESS, "查询成功", roleVOList);
    }

    /**
     * 删除角色对应的权限
     *
     * @param roleId
     * @param menusId
     * @return
     */
    @Override
    public ResultUtil deleteMenus(String roleId, int menusId) {
        List<Menus> ms = menusRepository.findAllByMenuPid(menusId);
        roleRepository.deleteRoleRinghtByMenusId(roleId, menusId);
        List<Menus> list = roleRepository.findById(roleId).get().getMenus();
        List tree = new ArrayList<>(menusServies.formatMenusTree(list).values());
        return new ResultUtil(ResultCodeConstant.SUCCESS, "删除成功！", tree);
    }

    /**
     * 给角色授权功能
     * @param roleId
     * @param rids
     * @return
     */
    @Override
    public ResultUtil roleAuthorization(String roleId, Integer[] rids) {
        List<Integer> list = menusRepository.findMenusByRole(roleId);
        for (int rid : rids) {
            // 判断是否存在，若已经存在，则跳过
            if (!list.contains(rid)) {
                roleRepository.saveRoleRinght(roleId, rid);
            }
        }
        return new ResultUtil(ResultCodeConstant.SUCCESS, "授权成功！");
    }

    /**
     * 获取所有的角色列表
     * @return
     */
    @Override
    public ResultUtil gitAllRoleInfo() {
        List<Role> list = roleRepository.findAll();
        if (list.isEmpty()) {
            return new ResultUtil(ResultCodeConstant.FAIL, "角色列表获取失败！");
        }
        return new ResultUtil(ResultCodeConstant.SUCCESS, "角色列表获取成功！", list);
    }

    /**
     * 添加新角色
     * @param role
     * @return
     */
    @Override
    public ResultUtil addRole(Role role) {
        Role result = roleRepository.save(role);
        if (result == null) {
            return new ResultUtil(ResultCodeConstant.FAIL, "角色添加失败！");
        }
        return new ResultUtil(ResultCodeConstant.SUCCESS, "角色添加成功！", result);
    }

    /**
     * 修改角色信息
     * @param role
     * @return
     */
    @Override
    public ResultUtil editRoleInfo(Role role) {
        Role result = roleRepository.save(role);
        if (result == null) {
            return new ResultUtil(ResultCodeConstant.FAIL, "角色信息修改失败！");
        }
        return new ResultUtil(ResultCodeConstant.SUCCESS, "角色信息修改成功！", result);
    }

    /**
     * 根据角色ID查询角色信息
     * @return
     * @param roleId
     */
    @Override
    public ResultUtil gitRoleInfo(String roleId) {
        Role role =roleRepository.findById(roleId).get();
        if (role == null) {
            return new ResultUtil(ResultCodeConstant.FAIL, "角色信息查询失败！");
        }
        return new ResultUtil(ResultCodeConstant.SUCCESS, "角色信息查询成功！", role);
    }

    /**
     * 删除角色
     * @param roleId
     * @return
     */
    @Override
    public ResultUtil deleteRole(String roleId) {
        roleRepository.deleteById(roleId);
        return new ResultUtil(ResultCodeConstant.SUCCESS, "角色删除成功！");
    }

}
